/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.comandos.admin;

import br.ufc.quixada.adrs.interfaces.Comando;
import br.ufc.quixada.adrs.model.Adrs;
import br.ufc.quixada.adrs.model.Contato;
import br.ufc.quixada.adrs.service.AdrsService;
import br.ufc.quixada.adrs.service.EstadoService;
import br.ufc.quixada.adrs.service.MunicipioService;
import br.ufc.quixada.adrs.util.UtilAdrs;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ismaily
 */
public class CmdEditarAdrs implements Comando {

    public String executa(HttpServletRequest request, HttpServletResponse response) {

        HttpSession session = request.getSession();
        Long id = Long.parseLong(request.getParameter("id"));
        Adrs adrs = new AdrsService().getAdrsById(id);
        List<Contato> contatos = adrs.getUsuario().getContatos();


        for (Contato c : contatos) {
            if (!UtilAdrs.isNumber(c.getNumero())) {
                c.setNumero("");
            }
        }

        session.setAttribute("adrs", adrs);
        session.setAttribute("nomeComando", "Alterar");
        session.setAttribute("estados", new EstadoService().getAllEstados());
        session.setAttribute("municipios", new MunicipioService().getAllMunicipios());
        return "/admin/admin_add_adrs.jsp";
    }
}
